<template>
  <div class="image-uploader">
    <!-- 大图区域 -->
    <div class="main-image-area">
      <div v-if="images.length === 0" class="image-placeholder" @click="onAddPictures">
        <span class="placeholder-text">{{ placeholderText }}</span>
      </div>
      <img v-else :src="images[currentIndex]" @click="onViewPictures(currentIndex)" class="main-image" />
    </div>

    <!-- 缩略图区域 -->
    <div class="thumbnail-section">
      <div ref="thumbnailScroll" class="thumbnail-container">
        <div v-for="(image, index) in images" :key="index" class="thumbnail-item">
          <img :src="image" class="thumbnail-image" :class="{ 'selected': currentIndex === index }" @click="onViewPictures(index)" />
          <button class="thumbnail-remove" @click.stop="onRemovePicture(index)">×</button>
        </div>
        <!-- 上传按钮 -->
        <div v-if="images.length < maxCount" class="upload-button-container">
          <img :src="uploadIcon" @click="onAddPictures" class="upload-button" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue';

interface Props {
  images: string[];
  maxCount?: number;
  placeholderText?: string;
  uploadIcon?: string;
}

interface Emits {
  'update:images': [images: string[]];
  'add': [];
  'remove': [index: number];
  'view': [index: number];
}

const props = withDefaults(defineProps<Props>(), {
  maxCount: 9,
  placeholderText: '点击添加照片',
  uploadIcon: '/src/assets/images/addpic.svg'
});

const emit = defineEmits<Emits>();
const thumbnailScroll = ref<HTMLDivElement>();
// 当前选中的图片索引
const currentIndex = ref(0);

// 监听图片数组变化，自动滚动到最右侧
watch(() => props.images.length, (newLength, oldLength) => {
  // 当图片数组变化时，如果当前选中的索引超出范围，则重置为0
  if (newLength > 0 && currentIndex.value >= newLength) {
    currentIndex.value = 0;
  }
  if (thumbnailScroll.value) {
    // 给DOM一点时间更新
    setTimeout(() => {
      if (thumbnailScroll.value) {
        thumbnailScroll.value.scrollLeft = thumbnailScroll.value.scrollWidth;
      }
    }, 100);
  }
});

// 添加图片
async function onAddPictures() {
  if (props.images.length >= props.maxCount) {
    alert(`最多只能上传${props.maxCount}张图片`);
    return;
  }
  emit('add');
}

// 查看图片
function onViewPictures(index: number) {
  currentIndex.value = index;
}

// 删除图片
function onRemovePicture(index: number) {
  emit('remove', index);
}
</script>

<style scoped>
.image-uploader {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

/* 大图区域 */
.main-image-area {
  width: 100%;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  overflow: hidden;
  background-color: var(--bg-secondary);
  border-radius: 8px;
}

.image-placeholder {
  width: 200px;
  height: 200px;
  border: 2px dashed var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background-color: var(--bg-secondary);
}

.placeholder-text {
  color: var(--text-secondary);
  font-size: 14px;
  text-align: center;
  padding: 10px;
}

.main-image {
  border-radius: 8px;
  object-fit: cover;
  max-width: 100%;
  max-height: 100%;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 缩略图区域 */
.thumbnail-section {
  width: 100%;
}

.thumbnail-container {
  display: flex;
  gap: 10px;
  padding: 10px 0;
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) var(--bg-secondary);
  white-space: nowrap;
}

.thumbnail-container::-webkit-scrollbar {
  height: 6px;
}

.thumbnail-container::-webkit-scrollbar-track {
  background: var(--bg-secondary);
  border-radius: 3px;
}

.thumbnail-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.thumbnail-container::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

.thumbnail-item {
  position: relative;
  width: 80px;
  height: 80px;
  flex-shrink: 0;
}

.thumbnail-image {
  width: 100%;
  height: 100%;
  border-radius: 4px;
  object-fit: cover;
  cursor: pointer;
  border: 1px solid var(--border);
  transition: all 0.2s ease;
}

.thumbnail-image.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(63, 136, 255, 0.2);
}

.thumbnail-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background-color: #000000;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  z-index: 100;
}

.thumbnail-remove:hover {
  background-color: var(--danger-hover);
}

/* 上传按钮 */
.upload-button-container {
  flex-shrink: 0;
}

.upload-button {
  width: 80px;
  height: 80px;
  border: 2px dashed var(--border);
  border-radius: 4px;
  cursor: pointer;
  background-color: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.upload-button:hover {
  border-color: var(--primary);
  background-color: var(--bg-hover);
}
</style>